package com.collection;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Queue <Item> implements Iterable<Item>{


    private Node <Item> first;
    private Node <Item> last;
    private int n;

    private static class Node<Item> {

        private Item item;
        private Node<Item> next;
    }


    public Queue() {
        first = null;
        last = null;
        n = 0;
    }

    public void enqueue(Item item) {
        Node oldLast = last;
        last = new Node<>();
        last.item = item;
        if (isEmpty()) {
            first = last;
        } else {
            oldLast.next = last;
        }
        n++;
    }

    public Item dequeue() {

        if(isEmpty()) {
            throw new NoSuchElementException("Queue underflow");
        }

        Item item = first.item;
        first = first.next;
        n--;
        if (isEmpty()) last = null;
        return item;
    }

    public int size() {
        return n;
    }



    public boolean isEmpty() {

        return first == null;
    }

    @Override
    public Iterator<Item> iterator() {
        return null;
    }
}
